<?php
/**
 * Class that operate on table 'campo'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2009-08-25 19:00
 */
class CampoDAO{

        /**
     * Get Domain object by primry key
     *
     * @param String $id primary key
     * @return CampoMySql
     */
    public function load($id){
            $sql = 'SELECT * FROM campo WHERE codCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($id);
            return $this->getRow($sqlQuery);
    }

    /**
     * Get all records from table
     */
    public function queryAll(){
            $sql = 'SELECT * FROM campo';
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

    /**
     * Get all records from table ordered by field
     *
     * @param $orderColumn column name
     */
    public function queryAllOrderBy($orderColumn){
            $sql = 'SELECT * FROM campo ORDER BY '.$orderColumn;
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

    /**
     * Delete record from table
     * @param campo primary key
     */
    public function delete($codCampo){
            $sql = 'DELETE FROM campo WHERE codCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($codCampo);
            return $this->executeUpdate($sqlQuery);
    }

    /**
     * Insert record to table
     *
     * @param CampoMySql campo
     */
    public function insert($campo){
            $sql = 'INSERT INTO campo (codTipo, nomeCampo, obrigatorio, dicaPreenchimento, funcaoValidacao, posicao) VALUES (?, ?, ?, ?, ?, ?)';
            $sqlQuery = new SqlQuery($sql);

            $sqlQuery->set($campo->codTipo);
            $sqlQuery->set($campo->nomeCampo);
            $sqlQuery->set($campo->obrigatorio);
            $sqlQuery->set($campo->dicaPreenchimento);
            $sqlQuery->set($campo->funcaoValidacao);
            $sqlQuery->set($campo->posicao);

            $id = $this->executeInsert($sqlQuery);
            $campo->id = $id;
            return $id;
    }

    /**
     * Update record in table
     *
     * @param CampoMySql campo
     */
    public function update($campo){
            $sql = 'UPDATE campo SET codTipo = ?, nomeCampo = ?, obrigatorio = ?, dicaPreenchimento = ?, funcaoValidacao = ?, posicao = ? WHERE codCampo = ?';

            $sqlQuery = new SqlQuery($sql);

            $sqlQuery->set($campo->codTipo);
            $sqlQuery->set($campo->nomeCampo);
            $sqlQuery->set($campo->obrigatorio);
            $sqlQuery->set($campo->dicaPreenchimento);
            $sqlQuery->set($campo->funcaoValidacao);
            $sqlQuery->set($campo->posicao);

            $sqlQuery->setNumber($campo->codCampo);
            return $this->executeUpdate($sqlQuery);
    }
    
	/**
     * Update record in table
     *
     * @param CampoMySql campo
     */
    public function updateDadosCampo($campo){
            $sql = 'UPDATE campo SET nomeCampo = ?, obrigatorio = ?, dicaPreenchimento = ? WHERE codCampo = ?';

            $sqlQuery = new SqlQuery($sql);

            $sqlQuery->set($campo->nomeCampo);
            $sqlQuery->set($campo->obrigatorio);
            $sqlQuery->set($campo->dicaPreenchimento);

            $sqlQuery->setNumber($campo->codCampo);
            return $this->executeUpdate($sqlQuery);
    }

    /**
     * Query
     */
    public function queryByCodTipo($value){
            $sql = 'SELECT * FROM campo WHERE codTipo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

    public function queryBynomeCampo($value){
            $sql = 'SELECT * FROM campo WHERE nomeCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

    public function queryByObrigatorio($value){
            $sql = 'SELECT * FROM campo WHERE obrigatorio = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

    public function queryByDicaPreenchimento($value){
            $sql = 'SELECT * FROM campo WHERE dicaPreenchimento = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

    public function queryByFuncaoValidacao($value){
            $sql = 'SELECT * FROM campo WHERE funcaoValidacao = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

	public function queryByPosicao($value){
            $sql = 'SELECT * FROM campo WHERE posicao = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

	public function queryByFormulario($value)
	{
            $sql = 'SELECT DISTINCT c.* FROM campo AS c INNER JOIN formulariocampo fc ON (fc.codCampo = c.codCampo) WHERE fc.codFormulario = ? ORDER BY c.posicao';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->getList($sqlQuery);
    }

    public function updateOrdenaCampos($ordem, $codCampo)
    {
    	$sql = 'UPDATE campo SET posicao = ? WHERE codCampo = ?';
    	$sqlQuery = new SqlQuery($sql);
    	$sqlQuery->setNumber($ordem);
    	$sqlQuery->setNumber($codCampo);

    	$this->execute($sqlQuery);
    }

    public function deleteByCodTipo($value){
            $sql = 'DELETE FROM campo WHERE codTipo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($value);
            return $this->executeUpdate($sqlQuery);
    }

    public function deleteBynomeCampo($value){
            $sql = 'DELETE FROM campo WHERE nomeCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->executeUpdate($sqlQuery);
    }

    public function deleteByObrigatorio($value){
            $sql = 'DELETE FROM campo WHERE obrigatorio = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($value);
            return $this->executeUpdate($sqlQuery);
    }

    public function deleteByDicaPreenchimento($value){
            $sql = 'DELETE FROM campo WHERE dicaPreenchimento = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->executeUpdate($sqlQuery);
    }

    public function deleteByFuncaoValidacao($value){
            $sql = 'DELETE FROM campo WHERE funcaoValidacao = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->executeUpdate($sqlQuery);
    }

    public function deleteByPosicao($value){
            $sql = 'DELETE FROM campo WHERE posicao = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->set($value);
            return $this->executeUpdate($sqlQuery);
    }



    /**
     * Read row
     *
     * @return CampoMySql
     */
    protected function readRow($row){
            $campo = new Campo();

            $campo->codCampo = $row['codCampo'];
            $campo->codTipo = $row['codTipo'];
            $campo->nomeCampo = $row['nomeCampo'];
            $campo->obrigatorio = $row['obrigatorio'];
            $campo->dicaPreenchimento = $row['dicaPreenchimento'];
            $campo->funcaoValidacao = $row['funcaoValidacao'];
            $campo->posicao = $row['posicao'];

            return $campo;
    }

    protected function getList($sqlQuery){
            $tab = QueryExecutor::execute($sqlQuery);
            $ret = array();
            for($i=0;$i<count($tab);$i++){
                    $ret[$i] = $this->readRow($tab[$i]);
            }
            return $ret;
    }

    /**
     * Get row
     *
     * @return CampoMySql
     */
    protected function getRow($sqlQuery){
            $tab = QueryExecutor::execute($sqlQuery);
            return $this->readRow($tab[0]);
    }


    /**
     * Execute sql query
     */
    protected function execute($sqlQuery){
            return QueryExecutor::execute($sqlQuery);
    }


    /**
     * Execute sql query
     */
    protected function executeUpdate($sqlQuery){
            return QueryExecutor::executeUpdate($sqlQuery);
    }

    /**
     * Insert row to table
     */
    protected function executeInsert($sqlQuery){
            return QueryExecutor::executeInsert($sqlQuery);
    }
}
?>
